const express = require('express');
const router = express.Router();
const r=require("../biz/users")
const svgCaptcha = require('svg-captcha');

/* GET users listing. */
router.get('/login', function(req, res) {

   r.login(req, res)
});

router.get('/getUserInfo', function(req, res) {

    r.getUserInfo(req, res)
});

router.post('/regist', function(req, res) {
    r.regist(req, res)
});



router.get('/getCaptcha', function(req, res, next) {
    var captcha = svgCaptcha.create({
        // 翻转颜色
        inverse: false,
        // 字体大小
        fontSize: 60,
        // 噪声线条数
        noise: 2,
        // 宽度
        width: 200,
        // 高度
        height:100,
    });
    // 保存到session,忽略大小写
    req.session.captcha = captcha.text.toLowerCase();
    console.log(req.session.captcha); //0xtg 生成的验证码
    res.setHeader('Content-Type', 'image/svg+xml');
    res.write(String(captcha.data));
    res.end()
})

router.post('/getCaptcha', function(req, res) {
    let captcha =req.body.captcha
    if (captcha == req.session.captcha) {
        res.send()
    }else {
        throw  new Error("验证失败")
    }
})
module.exports = router;
